【BFS】推箱子问题

题目:大家一定玩过“推箱子”这个经典的游戏。具体规则就是在一个N*M的地图上,有1个玩家、1个箱子、1个目的地以及若干障碍,其余是空地。玩家可以往上下左右4个方向移动,但是不能移动出地图或者移动到障碍里去。

        如果往这个方向移动推到了箱子,箱子也会按这个方向移动一格,当然,箱子也不能被推出地图或推到障碍里。当箱子被推到目的地以后,游戏目标达成。

        现在告诉你游戏开始是初始的地图布局,请你求出玩家最少需要移动多少步才能够将游戏目标达成。 

输入描述: 
每个测试输入包含1个测试用例 
第一行输入两个数字N,M表示地图的大小。其中0<NM<=80<N,M<=8。 
接下来有N行,每行包含M个字符表示该行地图。其中 . 表示空地、X表示玩家、*表示箱子、#表示障碍、@表示目的地。 
每个地图必定包含1个玩家、1个箱子、1个目的地。

输出描述: 

输出一个数字表示玩家最少需要移动多少步才能将游戏目标达成。当无论如何达成不了的时候,输出-1。


思路:经典的BFS,状态有四个维:玩家x坐标,玩家y坐标,箱子x坐标,箱子y坐标

代码如下:

//hero X;dest @;box *;block #;space .;
#include <iostream>
#include<queue>
#include<string>
using namespace std;
char maze[10][10];
int mark[10][10][10][10];
int dir[4][2] = { 1,0,0,1,-1,0,0,-1 };
int M, N;
struct node {
	int hr, hc, br, bc, depth;
	node(int hr, int hc, int br, int bc, int depth) :hr(hr), hc(hc), br(br), bc(bc), depth(depth) {}
};
int bfs(node& start);
int main() {
	while (cin >> M >> N) {
		node start(0, 0, 0, 0, 0);
		for(int i=0;i<M;i++)
			for (int j = 0; j < N; j++) {
				char c;
				cin >> c;
				maze[i][j] = c;
				if (c == 'X') {
					start.hr = i;
					start.hc = j;
				}
				if (c == '*') {
					start.br = i;
					start.bc = j;
				}
			}
		cout << bfs(start) << endl;
	}
	return 0;
}

BFS部分直接套用模板,唯一要注意的是碰到箱子后的处理方式

int bfs(node& start) {
	queue<node> q;
	q.push(start);
	mark[start.hr][start.hc][start.br][start.bc] = 1;
	while ((!q.empty())) {
		node head = q.front();
		q.pop();
		if (maze[head.br][head.bc] == '@')
			return head.depth;
		for (int i = 0; i < 4; i++) {
			int hnr = head.hr + dir[i][0];
			int hnc = head.hc + dir[i][1];
			int bnr = head.br;
			int bnc = head.bc;
			if (hnr < 0 || hnr >= M || hnc < 0 || hnc >= N)//人越界
				continue;
			if (maze[hnr][hnc] == '#')//人碰到障碍物
				continue;
			if (hnr==head.br&&hnc==head.bc) {
				bnr = bnr + dir[i][0];
				bnc = bnc + dir[i][1];
				if (bnr < 0 || bnr >= M || bnc < 0 || bnc >= N)//箱子越界
					continue;
				if (maze[bnr][bnc] == '#')//箱子碰到障碍物
					continue;
			}
			if (mark[hnr][hnc][bnr][bnc] == 1)//状态已被访问过
				continue;
			q.push(node(hnr, hnc, bnr, bnc, head.depth + 1));
			mark[hnr][hnc][bnr][bnc] = 1;
		}
	}
	return -1;
}





  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用BFS算法实现推箱子游戏的C++代码示例: ```cpp #include <iostream> #include <queue> #include <vector> using namespace std; struct State { int bx, by; // 箱子的位置 int px, py; // 人的位置 int steps; // 步数 }; int main() { int n, m; // 地图的行数和列数 cin >> n >> m; vector<vector<char>> map(n, vector<char>(m)); // 地图 vector<vector<vector<vector<bool>>>> visited(n, vector<vector<vector<bool>>>(m, vector<vector<bool>>(n, vector<bool>(m, false)))); // 记录访问状态 int bx, by, px, py, tx, ty; // 箱子、人和目标位置的坐标 for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> map[i][j]; if (map[i][j] == 'B') { bx = i; by = j; } else if (map[i][j] == 'S') { px = i; py = j; } else if (map[i][j] == 'T') { tx = i; ty = j; } } } queue<State> q; q.push({bx, by, px, py, 0}); visited[bx][by][px][py] = true; int dx[] = {-1, 1, 0, 0}; // 上下左右四个方向的偏移量 int dy[] = {0, 0, -1, 1}; while (!q.empty()) { State curr = q.front(); q.pop(); if (curr.bx == tx && curr.by == ty) { cout << curr.steps << endl; // 输出最少步数 return 0; } for (int i = 0; i < 4; i++) { int nx = curr.bx + dx[i]; int ny = curr.by + dy[i]; int px = curr.bx - dx[i]; int py = curr.by - dy[i]; if (nx >= 0 && nx < n && ny >= 0 && ny < m && px >= 0 && px < n && py >= 0 && py < m && map[nx][ny] != '#' && map[px][py] != '#' && !visited[nx][ny][curr.bx][curr.by]) { q.push({nx, ny, curr.bx, curr.by, curr.steps + 1}); visited[nx][ny][curr.bx][curr.by] = true; } } } cout << -1 << endl; // 无法到达目标位置 return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值